package leetcode.code1865;

import java.util.HashMap;
import java.util.Map;

import leetcode.helper.H;

public class FindSumPairs {
	int[] nums1;
	int[] nums2;
	Map<Integer, Integer> hash;

	public FindSumPairs(int[] nums1, int[] nums2) {
		this.nums1 = nums1;
		this.nums2 = nums2;
		hash = new HashMap<>();
		// hash num1 会超时
		for (int i = 0; i < nums2.length; i++) {
			int num = nums2[i];
			hash.put(num, hash.getOrDefault(num, 0) + 1);
		}
	}

	public void add(int index, int val) {
		int num = nums2[index];
		int size = hash.get(num);
		if (size > 1) {
			hash.put(num, size - 1);
		}else {
			hash.remove(num);
		}
		nums2[index] += val;
		hash.put(nums2[index], hash.getOrDefault(nums2[index], 0) + 1);
	}

	public int count(int tot) {
		int ans = 0;
		for (int i = 0; i < nums1.length; i++) {
			int dif = tot - nums1[i];
			ans += hash.getOrDefault(dif, 0);
		}
		return ans;
	}

	public static void main(String[] args) {
//		["FindSumPairs","count","add","count","count","add","add","count"]
//				[[,[7],[3,2],[8],[4],[0,1],[1,1],[7]]
		FindSumPairs fsp = new FindSumPairs(H.str2array3("[1,1,2,2,2,3]"), H.str2array3("[1,4,5,2,5,4]"));
//		System.out.println(fsp.count(7));
		fsp.add(3, 2);
//		System.out.println(fsp.count(8));
		System.out.println(fsp.count(4));
	}
}
